package cn.jsc.user.controller;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.util.AjaxResult;
import cn.jsc.basic.util.PageList;
import cn.jsc.user.domain.User;
import cn.jsc.user.dto.RegisterDto;
import cn.jsc.user.query.UserQuery;
import cn.jsc.user.service.IUserServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@ResponseBody+@Controller
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")

public class UserController {

    @Autowired
    private IUserServiceI userService;

    //删除  根据id删除  localhost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            userService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    @PatchMapping//批量删除操作
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            userService.patchDelete(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //查询一个对象 根据id  localhost:8080/dept/1
    @GetMapping("/{id}")
    public User queryById(@PathVariable Long id){
        return userService.getById(id);
    }

    /**
     * /新增或修改  根据id来区分
     *  @RequestBody :前台请求体中的json数据会帮你转成对象
     *  localhost:8080/dept
     */
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if(user.getId()==null){
                userService.add(user);
            }else{
                userService.update(user);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //查询所有的对象 localhost:8080/dept
    @GetMapping
    public List<User> queryAll(){
        return userService.getAll();
    }

    //查询分页数据 localhost:8080/dept
    @PostMapping
    public PageList<User> queryData(@RequestBody UserQuery query){
        return userService.queryData(query);
    }

    /**
     * 用户的手机号注册
     * @param registerDto
     * @return
     */
    @ApiOperation(value = "用户的手机号注册",notes = "")
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody RegisterDto registerDto){
        try {
            userService.phoneRegister(registerDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败, "+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误, " + e.getMessage());
        }
    }
}
